package org.henzhi.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;



/**
 * 访问数据库公共类
 * @author 谢瑶
 *
 */
public class BaseDao {

	public Connection conn=null;
	public ResultSet rs=null;
	public PreparedStatement ps=null;
	 final String DRIVER=ConfigManager.getInstance().getValue("jdbc_driver");
	 final String URL=ConfigManager.getInstance().getValue("jdbc_url");
	 final String USERNAME=ConfigManager.getInstance().getValue("jdbc_user");
	 final String PASSWORD=ConfigManager.getInstance().getValue("jdbc_pwd");
	 //获取连接
	 public Connection getConn(){
		 try {
			Class.forName(DRIVER);
			conn=DriverManager.getConnection(URL,USERNAME,PASSWORD);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 return conn;
	 }
	 //公共的增删改fangfa
	 public int exeUpdate(String sql,Object[] param ) throws SQLException {
		 int row=0;
		 conn=getConn();
		 ps=conn.prepareStatement(sql);
		 if(param!=null){
			 for(int i=0; i<param.length;i++){
			ps.setObject(i+1, param[i]);
			 }
		 }
		 row=ps.executeUpdate();
	
		return row;
	}

	 
	 public ResultSet exeQuery(String sql,Object[] param ) throws SQLException {
		 conn=getConn();
		 ps=conn.prepareStatement(sql);
		 if(param!=null){
			 for(int i=0; i<param.length;i++){
			ps.setObject(i+1, param[i]);
			 }
		 }
		 rs=ps.executeQuery();
		
		return rs;
	}
	 public void closeAll() throws SQLException{
		 if(rs!=null){
			 rs.close();
		 }
		 if(ps!=null){
			 ps.close();
		 }
		 if(conn!=null){
			 conn.close();
		 }
	 }
}
